package dashboard.databll;

import java.util.HashSet;
import java.util.List;

import com.bj58.groupbuy.action.model.OperMenu;
import com.bj58.groupbuy.action.model.User;
import com.bj58.groupbuy.action.service.UserOperService;
import com.bj58.groupbuy.action.service.impl.UserOperServiceImpl;
import com.bj58.groupbuy.action.util.MyStore;

import dashboard.constant.EnumCate;

public class PriviledgeBLL {
	private static UserOperService userOperService = new UserOperServiceImpl();
	
	/**业务线细粒度权限检查
	 */
	public static String businessOperCheck(User user)throws Exception{
		String operShowBusi = ""; 
		if(MyStore.admin.equals(user.getFunction())){	//管理员权限
			return "total,zhaopin,fangchan,huangye,ershou,ershouche";
		}else{ 
			HashSet<Long> userOpersSet = userOperService.getUserOpers(user.getId());//用户全部细粒度权限
			List<OperMenu> operList = userOperService.getOperListByCate("全局控制", "业务线权限"); //该系列所有正常数据权限
			for (OperMenu operMenu : operList) { //如果用户细粒度权限中没有此类权限约束  即不做控制处理
				long operId = operMenu.getId();
				if(userOpersSet.contains(operId)){
					if(operId == 12){ //全部业务线权限
						return "total,zhaopin,fangchan,huangye,ershou,ershouche";
					}else{
						String name  = operMenu.getName();
						if(EnumCate.business_CN_Map.containsKey(name))
							operShowBusi = operShowBusi +operMenu.getName()+",";
					}
				}
			}
			if(operShowBusi.length()>0){
				operShowBusi = operShowBusi.substring(0,operShowBusi.lastIndexOf(","));
				if(operShowBusi.split(",").length >= 5){	//全部业务线权限
					return "total,zhaopin,fangchan,huangye,ershou,ershouche";
				}
			}
		}
		
		return operShowBusi;
	}
	
}
